package com.hpf.tool.redis.interfaces.impl;

import java.util.Map;

import com.hpf.tool.interfaces.ICallable;
import com.hpf.tool.redis.interfaces.IHBase;
import com.hpf.tool.redis.interfaces.abstracts.AHBaseJedis;

import redis.clients.jedis.Jedis;
/**
 * 
 * @author hpf
 *
 * @param <T>
 */
@SuppressWarnings("unchecked")
public abstract class HBaseJedis<T> extends AHBaseJedis implements IHBase<T>{

	@Override
	public boolean exists(final String key) {
		/*
		 * 校验：key 
		 */
		super.verifyKey(key);
		
		return super.callBack(new ICallable.ICallableIO<Jedis, Boolean>(){
			@Override
			public Boolean exce(Jedis jedis) {
				return jedis.exists(key);
			}
		});
	}

	@Override
	public void remove(final String key) {
		/*
		 * 校验：key 
		 */
		super.verifyKey(key);
		
		super.callBack(new ICallable.ICallableI<Jedis>(){
			@Override
			public void exce(Jedis jedis) {
				jedis.del(key);
			}
		});
	}

	@Override
	public T get(final String key) {
		/*
		 * 校验：key 
		 */
		super.verifyKey(key);
		
		return super.callBack(new ICallable.ICallableIO<Jedis, T>(){
			private T t = null;

			
			@Override
			public T exce(Jedis jedis) {
				if(String.class.isAssignableFrom(t.getClass())){
					return (T)jedis.get(key);
				}
				
				if(Map.class.isAssignableFrom(t.getClass())){
					return (T) jedis.hgetAll(key);
				}
				return null;
			}
		});
	}

	@Override
	public void expire(final String key, final int expireTime) {
		/*
		 * 校验：key 
		 */
		super.verifyKey(key);
		
		super.callBack(new ICallable.ICallableI<Jedis>(){
			@Override
			public void exce(Jedis jedis) {
				jedis.expire(key, expireTime);
			}
		});
	}
}
